package com.aos.erp.fs.storage;

import com.aos.erp.fs.model.SysFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class FileStorageAdapter implements FileStorageHandler {

    private List<FileStorageHandler> handlerList;

    @Resource
    public void setHandlerList(List<FileStorageHandler> handlerList) {
        this.handlerList = handlerList;
        if (this.handlerList != null) {
            //排序
            this.handlerList.sort((a, b) -> {
                return a.order() - b.order();
            });
        }
    }

    @Override
    public boolean save(SysFile sysFile, MultipartFile file, String type) {
        if (handlerList != null) {
            for (FileStorageHandler handler : handlerList) {
                try {
                    if (handler.canHandler(sysFile, file, type)) {
                       /*
                        boolean isContinue = handler.save(sysFile, file);
                        if (!isContinue) {
                            break;
                        }
                        */
                        handler.save(sysFile, file, type);
                        //文件存储成功
                        return true;
                    }
                } catch (Exception ex) {
                    log.error(ex.getMessage());
                }
            }
        }
        return false;
    }


    @Override
    public boolean canHandler(SysFile sysFile, MultipartFile file, String type) {
        return false;
    }

}
